/*
 * The <code>TCache</code> uses a hash map <code>tc_map</code> to manages all the <code>TBlock</code> and <code>VBlock</code>.
 * Also , it assigns the executable memory space for storing the translated code. 
 */
#ifndef TCACHE_H
#define TCACHE_H

#include "crossbit/common.h"
#include "crossbit/MemoryManager.h"
#include "crossbit/TBlock.h"
#include "crossbit/VBlock.h"
#include "crossbit/HashMap.h"

//Assign a number divided by 32, show how many TBlock is allocated at one time
#define appnum 128
//Assign a number divided by 4, assess how many instructions in one block in average
#define avginblk 20		

namespace crossbit {

    class TCache {
        public:

            TCache( XTUint32 size, MemoryManager *mm );
            virtual ~TCache();
            // Return pointer to TBlock, NULL if fails.
            virtual TBlock *requestTBlock();
            // End crreate the cache item "spc,tblock,vblock"
            virtual void submitTBlock(XTMemAddr spc, TBlock *tblock, VBlock *vblock);
            // Lookup the TBlock from TCache with *Source PC*
            virtual TBlock *lookupTBlock(XTMemAddr spc);
            virtual VBlock *lookupVBlock(XTMemAddr spc);
            void eraseTBlock(TBlock *tblock);
            virtual void replacePolicy() {}
        protected:
            XTUint32		tc_pool_id; //An ID number assigned by MemoryManager when to apply a memory section to store TBlock objects. 
            XTUint32		tc_pool_state_id; //Likewise, to store objects' states

            XTUint32 		tc_size; //The size of target code cache which really store the translated codes
            MemoryManager		*tc_mm;

            HashMap			tc_map; //The hash table which maitains the relationship between SPC and TPC
            XTMemAddr		tc_tcode; //The started address of all target codes
            XTMemAddr		tc_cur_tcode;	//The address of current empty room where new target code can be stored

            TBlock          *tb_top;		//The top pointer to the TBlock object array
            XTUint32        tb_total;		//The largest number of TBlocks

            XTUint32		*tb_state;		//The top pointer to state_word arrary
            XTUint32		*tb_state_grp;		//The top pointer to group_state_word arrary

            XTUint32		tb_cur_init;		//The current number of TBlock objects which have been initialized
            XTUint32		tb_cur_avail;		//The current number of free TBlock objects
    };
}

#endif
